package com.yuyou.fn.electronicinvoice.service.impl;

import com.yuyou.fn.educational.service.ICourseClassService;
import com.yuyou.fn.educational.service.IStudentRegService;
import com.yuyou.fn.electronicinvoice.service.IElectronicInvoiceService;
import com.yuyou.fn.electronicinvoice.service.IFinanceSubjectService;
import com.yuyou.fn.electronicinvoice.vo.InvoiceDetailParamter;
import com.yuyou.fn.electronicinvoice.vo.InvoiceParamter;
import com.yuyou.fn.electronicinvoice.webservice.IEliWebServiceStub;
import org.apache.axis2.AxisFault;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.rmi.RemoteException;
import java.util.List;

/**
 * FinanceSubjectCampus 表数据服务层接口实现类
 */
@Service
public class ElectronicInvoiceServiceImpl implements IElectronicInvoiceService {

    @Value("${invoice.webservice.url}")
    private String invoiceWebserviceUrl;

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private ICourseClassService courseClassService;

    @Resource
    private IFinanceSubjectService financeSubjectService;


    @Override
    public IEliWebServiceStub.ReturnElectronice makeOut(InvoiceParamter invoiceParamter) throws AxisFault,RemoteException {

        IEliWebServiceStub eliWebServiceStub = new IEliWebServiceStub(invoiceWebserviceUrl);
        IEliWebServiceStub.InvEli invEli = new IEliWebServiceStub.InvEli();
        IEliWebServiceStub.ElectroniceInfo electroniceInfo = new IEliWebServiceStub.ElectroniceInfo();
        invEli.setIn0(electroniceInfo);
        //发票请求唯一流水号
        electroniceInfo.setFPQQLSH(invoiceParamter.getInvoiceId());

        //订单号
        electroniceInfo.setDDH(invoiceParamter.getOrderId());


        //开票方纳税人识别号
        electroniceInfo.setKP_NSRSBH(invoiceParamter.getInvoicerIdentificationNo());


        //开票方名称
        electroniceInfo.setKP_NSRMC(invoiceParamter.getInvoicerIdentificationName());

        //开票方电子档案号
        electroniceInfo.setNSRDZDAH(invoiceParamter.getInvoicerElectronicFile());

        //税务机构代码
        electroniceInfo.setSWJG_DM(invoiceParamter.getTaxCode());


        //代开标志
        electroniceInfo.setDKBZ(invoiceParamter.getIssueSign());

        //主要开票项目
        electroniceInfo.setKPXM(invoiceParamter.getInvoiceProject());


        //销货方识别号
        electroniceInfo.setXHF_NSRSBH(invoiceParamter.getSaleNo());

        //销货方名称
        electroniceInfo.setXHF_MC(invoiceParamter.getSaleName());

        //销货方地址
        electroniceInfo.setXHF_DZ(invoiceParamter.getSaleAddress());

        //销货方电话
        electroniceInfo.setXHF_DH(invoiceParamter.getSalePhone());

        //销货方银行账号
        electroniceInfo.setXHF_YHZH(invoiceParamter.getSaleBankName());

        //购货方名称
        electroniceInfo.setGHF_MC(invoiceParamter.getBuyerName());

        //购货方识别号
        electroniceInfo.setGHF_NSRSBH(invoiceParamter.getBuyerNo());

        //购货方地址
        electroniceInfo.setGHF_DZ(invoiceParamter.getBuyerAddress());

        //购货方省份
        electroniceInfo.setGHF_SF(invoiceParamter.getBuyerProvince());

        //'购货方固定电话
        electroniceInfo.setGHF_GDDH(invoiceParamter.getBuyerFixedPhone());

        //购货方手机
        electroniceInfo.setGHF_SJ(invoiceParamter.getBuyerPhone());


        //购货方邮箱
        electroniceInfo.setGHF_EMAIL(invoiceParamter.getBuyerEmail());

        //购货方企业类型,01:企业02：机关事业单位03：个人04：其它
        electroniceInfo.setGHFQYLX(invoiceParamter.getBuyerType());

        //行业代码
        electroniceInfo.setHY_DM(invoiceParamter.getIndustryCode());

        //行业名称
        electroniceInfo.setHY_MC(invoiceParamter.getIndustryName());

        //开票员
        electroniceInfo.setKPR(invoiceParamter.getInvoiceMember());

        //收款员
        electroniceInfo.setSKR(invoiceParamter.getCashier());

        //复核人
        electroniceInfo.setFHR(invoiceParamter.getReviewMember());

        //开票类型 1正票、2红票
        electroniceInfo.setKPLX(invoiceParamter.getInvoiceType());

        //原发票代码,如果操作代码不是10时候都是必录
        electroniceInfo.setYFP_DM(invoiceParamter.getOriginalInvoiceCode());

        //原发票号码,如果操作代码不是10时候都是必录
        electroniceInfo.setYFP_HM(invoiceParamter.getOriginalInvoiceNumber());

        //0正常冲红(电子发票) 1特殊冲红(冲红纸质等)，在支持特殊冲红之前默认为”0”
        electroniceInfo.setTSCHBZ(invoiceParamter.getRushRedReason());

        //操作代码 10正票正常开具 11正票错票重开 20退货折让红票、21 错票重开红票、22换票冲红（全冲红电子发票，开具纸质发票）
        electroniceInfo.setCZDM(invoiceParamter.getOperationCode());


        //冲红原因
        electroniceInfo.setCHYY(invoiceParamter.getRushRedReason());


        //价税合计金额
        electroniceInfo.setKPHJJE(invoiceParamter.getTaxTotalAmount());

        //合计不含税金额。所有商品行不含税金额之和 HJBHSJE
        if (invoiceParamter.getNotTaxAmount() != null) {
            electroniceInfo.setHJBHSJE(invoiceParamter.getNotTaxAmount());
        }


        //合计税额。所有商品行税额之和。 HJSE
        if (invoiceParamter.getTotalTaxAmount() != null) {
            electroniceInfo.setHJSE(invoiceParamter.getTotalTaxAmount());
        }


        //税收分类编码版
        electroniceInfo.setBMB_BBH(invoiceParamter.getTaxClassificationCode());

        IEliWebServiceStub.ArrayOfElectroniceDetail arrayOfElectroniceDetail = new IEliWebServiceStub.ArrayOfElectroniceDetail();

        List<InvoiceDetailParamter> invoiceDetailParamters = invoiceParamter.getInvoiceDetailParamterList();
        for (InvoiceDetailParamter invoiceDetailParamter : invoiceDetailParamters) {
            IEliWebServiceStub.ElectroniceDetail electroniceDetail = new IEliWebServiceStub.ElectroniceDetail();

            //项目名称
            electroniceDetail.setXMMC(invoiceDetailParamter.getProjectName());

            //项目单位
            electroniceDetail.setDW(invoiceDetailParamter.getProjectUnits());

            //规格型号
            electroniceDetail.setGGXH(invoiceDetailParamter.getSpecificationsModels());

            //项目数量
            electroniceDetail.setXMSL(invoiceDetailParamter.getProjectNum());

            //含税标志,表示项目单价和项目金额是否含税。0表示都不含税，1表示都含税。目前输入含税价,即标志为1
            electroniceDetail.setHSBZ(invoiceDetailParamter.getTaxLogo());

            //项目单价
            if (invoiceDetailParamter.getProjectUnitPrice() != null) {
                electroniceDetail.setXMDJ(invoiceDetailParamter.getProjectUnitPrice());
            }


            //项目编码
            electroniceDetail.setXMBM(invoiceDetailParamter.getProjectCode());

            //商品分类编码
            electroniceDetail.setSPBM(invoiceDetailParamter.getGoodCategoryCode());

            //自行编码
            electroniceDetail.setZXBM(invoiceDetailParamter.getAutoCode());

            //优惠政策标识,0：不使用，1使用
            electroniceDetail.setYHZCBS(invoiceDetailParamter.getIfDiscount());

            //零税率标识,空：非零税率，0：出口零税，1：免税，2：不征税，3普通零税率
            electroniceDetail.setLSLBS(invoiceDetailParamter.getZeroRateTag());

            //增值税特殊管理,当优惠政策标识是1时必填，例：免税
            electroniceDetail.setZZSTSGL(invoiceDetailParamter.getTaxEspecialManager());

            //项目金额,等于=单价*数量，根据含税标志，确定此金额是否为含税金额。目前为输入含税金额（负数票或者为折扣行时为负数）\
            if (invoiceDetailParamter.getProjectAmount() != null) {
                electroniceDetail.setXMJE(invoiceDetailParamter.getProjectAmount());
            }


            //税率,如果税率为0，表示免税,输入小数如0.17 表示17%
            electroniceDetail.setSL(invoiceDetailParamter.getTaxRate());

            //税额,小数点后2位，以元为单位精确到分负数票或者为折扣行时为负数）
            if (invoiceDetailParamter.getTaxAmount() != null) {
                electroniceDetail.setSE(invoiceDetailParamter.getTaxAmount());
            }


            //扣除额
            if (invoiceDetailParamter.getSubAmount() != null) {
                electroniceDetail.setSE(invoiceDetailParamter.getSubAmount());
            }


            arrayOfElectroniceDetail.addElectroniceDetail(electroniceDetail);

        }
        //发票明细
        electroniceInfo.setDetails(arrayOfElectroniceDetail);

        //备注
        electroniceInfo.setBZ(invoiceParamter.getRemark());

        IEliWebServiceStub.InvEliResponse invEliResponse = eliWebServiceStub.invEli(invEli);
        IEliWebServiceStub.ReturnElectronice returnElectronice = invEliResponse.getOut();
        return returnElectronice;


    }

    @Override
    public IEliWebServiceStub.ReturnElectronice findInvoiceDetail(InvoiceParamter invoiceParamter) throws RemoteException {
        IEliWebServiceStub eliWebServiceStub=new IEliWebServiceStub(invoiceWebserviceUrl);
        IEliWebServiceStub.QueryEliData queryEliData = new IEliWebServiceStub.QueryEliData();
        IEliWebServiceStub.ElectroniceInfo electroniceInfo=new IEliWebServiceStub.ElectroniceInfo();
        queryEliData.setIn0(electroniceInfo);

        electroniceInfo.setDDH(invoiceParamter.getOrderId());
        electroniceInfo.setFPQQLSH(invoiceParamter.getInvoiceId());
        electroniceInfo.setKP_NSRSBH(invoiceParamter.getInvoicerIdentificationNo());
        electroniceInfo.setXHF_NSRSBH(invoiceParamter.getSaleNo());

        IEliWebServiceStub.QueryEliDataResponse queryEliDataResponse = eliWebServiceStub.queryEliData(queryEliData);
        IEliWebServiceStub.ReturnElectronice returnElectronice = queryEliDataResponse.getOut();
        return returnElectronice;
    }
}